home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HPAVC
/
HPAVC CD-ROM.iso
/
MCASM.RAR
/
MC_ASM.EXE
/
WROX_ASM
/
CH12
/
COMMON
/
FORMATS.H
< prev
next >
Wrap
C/C++ Source or Header
|
1994-09-24
|
9KB
|
309 lines
/************************************************************************/
/* This header contains classes for image formats handling */
/* and some associated useful procedure */
/* supported formats BMP,TGA,GIF,PCX */
/* */
/* Kiselev Y. && E.Podvoysky from ^Z for WROX press book, 1994 */
/************************************************************************/
#ifndef FORMATS_H
#define FORMATS_H
#include "grfile.h"
#include "graph.h"
//----------------------------------- TARGA ----------------------------
typedef struct { // targa file header
BYTE ID_field_length;
char color_map_type;
BYTE image_type;
WORD first_colmap_entry;
WORD colmap_length;
char colmap_entry_size;
int x;
int y;
int width;
int height;
char bitsperpixel;
BYTE attrbits : 4;
BYTE right_to_left : 1;
BYTE top_to_bottom : 1;
BYTE interleave : 2;
} targa_header;
typedef colortype targa_palette[256];
class targa_file: public graph_file_reader { // TARGA file reader
private:
targa_header header;
int bytesperpixel,counter;
BOOL rle_comp;
public:
targa_file(char *fname); // open file, read headers and palette
// allocates source_line
virtual ~targa_file();
virtual int get_next_line(); // returns -1 if file is finished
};
class targa_file_saver: public graph_file_saver { // TARGA file saver
private:
targa_header header;
int bytesperpixel,counter,count_row;
BOOL rle_comp;
int *arr_ofs;
public:
targa_file_saver(char *fname, image_type dest_type_,
image_extention_type dest_ext_type_,
int width_,int height_,BGRpalette* pal,BOOL compress,
BOOL top_to_bottom_,BOOL left_to_right_);
virtual void update_palette(BGRpalette *pal);
virtual int put_next_line(BYTE* line); // returns -1 if file is finished
~targa_file_saver();
};
//----------------------------------- BMP ------------------------------
const WORD BMPSIGNATURE = 'M'*256+'B';
typedef struct {
WORD bf_type; //signature
long bf_size; //size of file in DWORD
WORD bf_reserved1, bf_reserved2;
long bf_offbits; //image start offset in byte
} bmp_file_header;
typedef struct {
long bi_size;
long bi_width;
long bi_height;
WORD bi_planes;
WORD bi_bit_count;
long bi_compression;
long bi_size_image;
long bi_xpels_per_meter;
long bi_ypels_per_meter;
long bi_clr_used;
long bi_clr_important;
} bmp_file_info_header;
class bmp_file: public graph_file_reader { // bitmap file reader
private:
bmp_file_header header;
bmp_file_info_header info;
public:
bmp_file(char *fname); //open file, check signature, read headers and palette
//allocates source_line
virtual ~bmp_file();
virtual int get_next_line(); //returns -1 if file is finished
};
void build_bmp_info(bmp_file_info_header &info,
image_type f_type, long width,long height);
class bmp_file_saver: public graph_file_saver{
bmp_file_info_header info;
void init(bmp_file_info_header *info_);
// is called from constructors
public:
bmp_file_saver(char *fname,image_type dest_type_,
BGRpalette *palette, bmp_file_info_header *info_);
bmp_file_saver(char *fname,image_type dest_type_,
int width_,int height_,BGRpalette * pal);
virtual void update_palette(BGRpalette *pal);
virtual int put_next_line(BYTE *line); //returns -1 if some error occurs
};
//----------------------------------- GIF ------------------------------
#define LZW_BITS 12
#define LZW_TABLE_SIZE 0x1000 // 1 << LZW_BITS
#define HASH_SIZE 5003
const char GIFSIGNATURE[3] = "GIF";
typedef struct {
char signature[3]; // = "GIF"
char version[3]; // = "87a" or "89a" (version)
} gif_header;
typedef struct {
int width;
int height;
BYTE sizeofGCT : 3; // size of Global Color Table
BYTE sortflag : 1; // sort flag
BYTE colorres : 3; // color resolution
BYTE GCTflag : 1; // Global Color Table flag
BYTE backcolor; // background color
BYTE pixelaspectratio; // pixel aspect ratio
} logical_screen_descriptor;
#define IMAGE_DESCRIPTOR 0x2C
typedef struct {
int x; // image left position
int y; // image top position
int width;
int height;
BYTE sizeofLCT : 3;
BYTE reserved : 2;
BYTE sortflag : 1; // sort flag
BYTE interlaceflag : 1; // interlace flag
BYTE LCTflag : 1; // Local Color Table flag
} image_descriptor;
class gif_file: public graph_file_reader { // GIF file reader
private:
gif_header header;
logical_screen_descriptor scrdesc;
image_descriptor imagedesc;
WORD *prefixcode;
BYTE *secondbyte;
BYTE *decodestack;
BYTE LZW_code_size;
int num_bits,clear_code,end_of_info,first_code,max_code;
DWORD bit_buffer;
int bit_count,counter;
BYTE blocksize,pixelvalue;
BOOL clear_flag;
WORD next_code,new_code,old_code;
BYTE *stack_top;
WORD getcode();
BYTE *decode(BYTE *stack_bottom, WORD code);
// gif uses papa's buffer in specifical way, so
// it allocates and disposes it in constructors and destructors
public:
gif_file(char *fname); // open file, check signature, read headers and palette
// allocates source_line
virtual ~gif_file();
virtual void seek_start(); // seek to image itself start (bottom line)
virtual int get_next_line(); // returns -1 if file is finished
};
class gif_file_saver: public graph_file_saver { // GIF file saver
private:
gif_header header;
logical_screen_descriptor scrdesc;
image_descriptor imagedesc;
int *code_value;
WORD *prefixcode;
BYTE *secondbyte;
BYTE LZW_code_size;
int num_bits,clear_code,end_of_info,first_code,max_code;
DWORD bit_buffer;
int bit_count;
WORD next_code,code;
BOOL first_byte;
void putcode(WORD code);
int find_match(int hash_prefix,WORD hash_symbol);
// gif uses papa's buffer in specifical way, so
// it allocates and disposes it in constructors and destructors
public:
gif_file_saver(char *fname, image_type dest_type_,
int width_,int height_,BGRpalette* pal);
// open file, write signature, write headers and palette
virtual void update_palette(BGRpalette *pal);
virtual ~gif_file_saver();
virtual int put_next_line(BYTE *line);
};
const char PCXSIGNATURE = 0x0A;
typedef struct {
char signature;
char version;
char encoding;
char bitsperpixel;
int x1;
int y1;
int x2;
int y2;
int Xres,Yres;
colortype palette[16];
char reserved;
char planes;
int bytesperline;
int palinterp;
int videoscrsizex;
int videoscrsizey;
char res[54];
} pcx_header;
class pcx_file: public graph_file_reader { // PCX file reader
private:
pcx_header header;
BYTE prev_c,rep,datab;
int real_len;
public:
pcx_file(char *fname); // open file, check signature, read headers and palette
// allocates source_line
virtual ~pcx_file();
virtual void seek_start(); // seek to image itself start (bottom line)
virtual int get_next_line(); // returns -1 if file is finished
long image_size(); // returns estimation of memory required to store image
};
class pcx_file_saver: public graph_file_saver { // PCX file saver
private:
pcx_header header;
public:
pcx_file_saver(char *fname, image_type dest_type_,
image_extention_type dest_ext_type_,
int width_,int height_,BGRpalette* pal); // open file, write headers
// allocates source_line
virtual void update_palette(BGRpalette *pal);
virtual int put_next_line(BYTE *line);
};
/*------------------*/
/* any file opening */
graph_file_reader *open_image_file(char *fname);
// if error occured returns null
// and sets format_file_error in non-zero value
graph_file_saver *open_saver_file(char *fname,
image_type dest_type,
image_extention_type dest_ext_type,
int width, int height,
BGRpalette* palptr,
BOOL top_to_bottom, // if possible
BOOL left_to_right);
// if error occured returns null
// and sets format_file_error in non-zero value
extern int format_file_error;
// 0 - Ok
// 1 - File not found
// 2 - Unknown file format
// 3 - Format header error
// 4 - Disk read / write error
char *format_file_error_report();
void choosevideomode(graph_file_reader *grf_r);
// set optimal (but 256 color) mode for image
void setpalette_by_BGR(BGRpalette pal);
// actually sets palette with values pal[i].x/4
// init simple standard palette for 256 colors mode
void stand_palette(VGApalette &pal);
// simplest way to preview truecolor
void put_TC_row(BYTE *rp, BYTE *gp, BYTE *bp,int x,int y,int xcount);
#endif